package com.haust.leyou.controller;

import com.haust.leyou.service.GoodsHtmlService;
import com.haust.leyou.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.Map;

/**
 * @Auther: csp1999
 * @Date: 2020/12/16/10:00
 * @Description:
 */
@Controller
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @Autowired
    private GoodsHtmlService goodsHtmlService;

    /**
     * 商品详情也页面
     *
     * @param id
     * @param model
     * @return
     */
    @GetMapping("/item/{id}.html")
    public String toItemPage(@PathVariable("id") Long id, Model model) {

        Map<String, Object> map = goodsService.loadData(id);

        model.addAllAttributes(map);

        // 页面静态化：调用createHTML(id) 方法生成静态页面并保存到服务器的nginx 对应目录下缓存
        // 通过多线程方式：
        goodsHtmlService.asyncExcute(id);
//        goodsHtmlService.createHTML(id);

        return "/item";
    }
}
